package com.edu.exam.entity.examination.paper;

import com.edu.exam.enums.system.FileType;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 特殊考试附件实体类
 * 存储特殊考试申请的相关附件信息
 *
 * @author S-01 Module Designer
 * @version 2.0
 * @since 2025-11-10
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
public class SpecialExamAttachment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "application_id", referencedColumnName = "id", nullable = false, foreignKey = @ForeignKey(name = "fk_special_exam_attachment_application"))
    private SpecialExamApplication application;

    @Column(name = "file_name", length = 255, nullable = false)
    private String fileName;

    @Column(name = "original_name", length = 255, nullable = false)
    private String originalName;

    @Column(name = "file_path", length = 500, nullable = false)
    private String filePath;

    @Column(name = "file_size", nullable = false)
    private Long fileSize;

    @Column(name = "file_type", length = 32, nullable = false)
    private String fileType;

    @Column(name = "mime_type", length = 128)
    private String mimeType;

    @Column(name = "description", length = 500)
    private String description;

    @CreationTimestamp
    @Column(name = "upload_time", nullable = false, updatable = false)
    private LocalDateTime uploadTime;

    @UpdateTimestamp
    @Column(name = "update_time")
    private LocalDateTime updateTime;

    @Column(name = "uploader_id", length = 64, nullable = false)
    private String uploaderId;

    @Column(name = "uploader_name", length = 128)
    private String uploaderName;

    @Column(name = "download_count", nullable = false)
    private Integer downloadCount = 0;

    @Column(name = "is_public", nullable = false)
    private Boolean isPublic = false;

    @Column(name = "checksum", length = 64)
    private String checksum;

    @Column(name = "ip_address", length = 45)
    private String ipAddress;

    @Column(name = "user_agent", length = 255)
    private String userAgent;

    @Column(name = "status", length = 32, nullable = false)
    private String status;

    /**
     * 获取关联的申请ID
     */
    @Transient
    public Long getApplicationId() {
        return application != null ? application.getId() : null;
    }

    /**
     * 获取格式化的文件大小
     */
    @Transient
    public String getFormattedFileSize() {
        if (fileSize == null) {
            return "0 B";
        }
        
        long size = fileSize;
        String[] units = {"B", "KB", "MB", "GB"};
        int unitIndex = 0;
        
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        
        return size + " " + units[unitIndex];
    }
}